extends VBoxContainer

@onready var camera_controller = get_node("/root/Main/CameraController")
@onready var camera = get_node("/root/Main/CameraController/Camera3D")

var fov_input: LineEdit
var rotation_x_input: LineEdit
var pos_x_input: LineEdit
var pos_y_input: LineEdit
var pos_z_input: LineEdit

var is_visible: bool = false

func _ready():
	visible = false
	setup_ui()
	connect_signals()

func setup_ui():
	add_theme_constant_override("separation", 10)
	
	var title = Label.new()
	title.text = "Camera Debug"
	title.add_theme_font_size_override("font_size", 18)
	title.add_theme_color_override("font_color", Color(1, 1, 0))
	add_child(title)
	
	fov_input = create_input_field("FOV:", "45")
	rotation_x_input = create_input_field("Rotation X:", "-45")
	pos_x_input = create_input_field("Position X:", "0")
	pos_y_input = create_input_field("Position Y:", "30")
	pos_z_input = create_input_field("Position Z:", "20")
	
	var apply_button = Button.new()
	apply_button.text = "Apply Changes"
	apply_button.pressed.connect(_on_apply_pressed)
	add_child(apply_button)

func create_input_field(label_text: String, default_value: String) -> LineEdit:
	var container = HBoxContainer.new()
	
	var label = Label.new()
	label.text = label_text
	label.custom_minimum_size.x = 80
	label.add_theme_font_size_override("font_size", 14)
	container.add_child(label)
	
	var input = LineEdit.new()
	input.text = default_value
	input.custom_minimum_size.x = 100
	input.add_theme_font_size_override("font_size", 14)
	container.add_child(input)
	
	add_child(container)
	return input

func connect_signals():
	fov_input.text_submitted.connect(_on_input_submitted)
	rotation_x_input.text_submitted.connect(_on_input_submitted)
	pos_x_input.text_submitted.connect(_on_input_submitted)
	pos_y_input.text_submitted.connect(_on_input_submitted)
	pos_z_input.text_submitted.connect(_on_input_submitted)

func _input(event):
	if event.is_action_pressed("debug_mode"):
		toggle_visibility()

func toggle_visibility():
	is_visible = !is_visible
	visible = is_visible
	if is_visible:
		update_values_from_camera()

func update_values_from_camera():
	if camera and camera_controller:
		fov_input.text = str(snapped(camera.fov, 0.1))
		rotation_x_input.text = str(snapped(rad_to_deg(camera_controller.rotation.x), 0.1))
		pos_x_input.text = str(snapped(camera_controller.position.x, 0.1))
		pos_y_input.text = str(snapped(camera_controller.position.y, 0.1))
		pos_z_input.text = str(snapped(camera_controller.position.z, 0.1))

func _on_input_submitted(_text: String):
	apply_camera_changes()

func _on_apply_pressed():
	apply_camera_changes()

func apply_camera_changes():
	if not camera or not camera_controller:
		return
		
	var new_fov = float(fov_input.text)
	var new_rotation_x = deg_to_rad(float(rotation_x_input.text))
	var new_pos_x = float(pos_x_input.text)
	var new_pos_y = float(pos_y_input.text)
	var new_pos_z = float(pos_z_input.text)
	
	camera.fov = new_fov
	camera_controller.rotation.x = new_rotation_x
	camera_controller.position = Vector3(new_pos_x, new_pos_y, new_pos_z)
	
	camera_controller.target_fov = new_fov
	camera_controller.target_rotation = Vector3(new_rotation_x, 0, 0)
	camera_controller.target_position = Vector3(new_pos_x, new_pos_y, new_pos_z)
	
	print("Camera updated - FOV: ", new_fov, ", Rot X: ", rad_to_deg(new_rotation_x), 
		  ", Pos: (", new_pos_x, ", ", new_pos_y, ", ", new_pos_z, ")")